package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.GuiguCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 * 分类接口的实现
 */
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;



	/**
	 * 获取全部分分类信息,并关联一级分类对应的二级分类,关联二级分类对应的三级分类
	 * @return
	 */
	@Override
	public List getBaseCategoryList() {
		//查询出所有的一级分类二级分类三级分类
		List<BaseCategoryView> categoryViewList = baseCategoryViewMapper.selectList(null);
		//根据一级分类分组
		Map<Long, List<BaseCategoryView>> category1Map =
				categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//遍历category1Map,获取一级分类列表
		return category1Map.entrySet().stream().map(category1->{
			//获取一级分类的id
			Long category1Key = category1.getKey();
			//获取value
			List<BaseCategoryView> category1Value = category1.getValue();
			//获取一级分类的名字
			String category1Name = category1Value.get(0).getCategory1Name();
			//根据二级分类id分组
			Map<Long, List<BaseCategoryView>> category2Map =
					category1Value.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//遍历category2Map,获取一级分类对应的二级分类列表
			List<JSONObject> c2List = category2Map.entrySet().stream().map(category2 -> {
				//获取二级分类的id
				Long category2Key = category2.getKey();
				//获取value
				List<BaseCategoryView> category2Value = category2.getValue();
				//获取二级分类的名字
				String category2Name = category2Value.get(0).getCategory2Name();
				//根据三级分类id分组
				Map<Long, List<BaseCategoryView>> category3Map =
						category2Value.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
				//遍历category3Map,获取二级分类对应的一级分类
				List<JSONObject> c3List = category3Map.entrySet().stream().map(category3 -> {
					//获取三级分类的id
					Long category3Key = category3.getKey();
					//获取三级分类的名字
					String category3Name = category3.getValue().get(0).getCategory3Name();
					//组装三级分类
					JSONObject c3 = new JSONObject();
					c3.put("categoryId",category3Key );
					c3.put("categoryName", category3Name);
					return c3;
				}).collect(Collectors.toList());

				//组装二级分类对应的三级分类
				JSONObject c2 = new JSONObject();
				c2.put("categoryId", category2Key);
				c2.put("categoryName", category2Name);
				c2.put("categoryChild", c3List);
				return c2;
			}).collect(Collectors.toList());
			//组装一级分类对应的二级分类
			JSONObject c1 = new JSONObject();
			c1.put("categoryId", category1Key);
			c1.put("categoryName", category1Name);
			c1.put("categoryChild",c2List);
			return c1;
		}).collect(Collectors.toList());
	}

	/**
	 * 根据三级分类id获取一级分类,二级分类,三级分类
	 *
	 * @param category3Id
	 * @return
	 */
	@GuiguCache(prefix = "getBaseCategoryView:")
	@Override
	public BaseCategoryView getBaseCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	/**
	 * 根据一级分类id获取标签列表
	 *
	 * @param category1Id 一级分类的id
	 * @return 标签列表
	 */
	@Override
	public List<BaseAttribute> getAttribute(Long category1Id) {
		List<BaseAttribute> attribute = baseAttributeMapper.selectBaseAttributeByCategory1Id(category1Id);
		return attribute;
	}

	/**
	 * 根据一级分类查询置顶前七的三级分类
	 *
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		return baseCategory3Mapper.findTopBaseCategory3(category1Id);
	}

	/**
	 * 根据一级分类id查询二级分类和三级分类
	 *
	 * @param category1Id
	 * @return
	 */
	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		//查询一级分类对应的所有二级分类和三级分类
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(
				new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
		//获取一级分类的名字
		String category1Name = baseCategoryViewList.get(0).getCategory1Name();
		//根据二级分类id分桶
		Map<Long, List<BaseCategoryView>> category2Map =
				baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		//获取一级分类的子分类
		List categoryChild1 = category2Map.entrySet().stream().map(categoryEntry2->{
			//获取二级分类id
			Long category2Id = categoryEntry2.getKey();
			//获取二级分类名称
			List<BaseCategoryView> baseCategoryViewList2 = categoryEntry2.getValue();
			String category2Name = baseCategoryViewList2.get(0).getCategory2Name();
			//根据三级分类id分桶
			Map<Long, List<BaseCategoryView>> category3Map =
					baseCategoryViewList2.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
			//获取二级分类对应的三级分类
			List categoryChild2 = category3Map.entrySet().stream().map(categoryEntry3->{
				//获取三级分类id
				Long category3Id = categoryEntry3.getKey();
				String category3Name = categoryEntry3.getValue().get(0).getCategory3Name();
				JSONObject c3Json = new JSONObject();
				c3Json.put("categoryId",category3Id);
				c3Json.put("categoryName",category3Name);
				return c3Json;
			}).collect(Collectors.toList());

			//封装子分类的返回结果
			JSONObject c2Json= new JSONObject();
			c2Json.put("categoryId",category2Id);
			c2Json.put("categoryName",category2Name);
			c2Json.put("categoryChild",categoryChild2);
			return c2Json;
		}).collect(Collectors.toList());

		//封装返回结果
		JSONObject result = new JSONObject();
		result.put("categoryId",category1Id);
		result.put("categoryName",category1Name);
		result.put("categoryChild",categoryChild1);
		return result;
	}
}
